﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class sy_Admin_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AdminCs.CheckLoginState();
    }
    protected void btnAdmin_Add_Click(object sender, EventArgs e)
    {
        int AdminType = Convert.ToInt32( DropDownList1.SelectedValue) ;
        DateTime strTime = DateTime.Now;
        string strUser = TextBox1.Text.ToString().Trim();
        string strPwd = TextBox2.Text.ToString().Trim();
        string strPwd2 = TextBox3.Text.ToString().Trim();

        Md5Pw md5 = new Md5Pw();//MD5加密
        string Pwd = md5.Md5(strPwd);


        if (AdminType == -1)
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('请选择管理员类型！');", true);
        }
        else if (strUser == "")
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('用户名不能为空！');", true);
        }
        else if (strPwd == "")
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('密码不能为空！');", true);
        }
        else if (strPwd2 == "")
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('请输入确认密码！');", true);
        }
        else
        {
            if (strPwd == strPwd2)//两次密码验证,如果一样
            {
                if (this.IsValid)//用户名是否存在验证
                {
                    SqlConnection conn = Class1.DBconnection();//返回连接对象
                    conn.Open();//打开连接
                    string strsql2 = "select * from SyAdmin where AdminName='" + strUser + "'";
                    SqlCommand cmd1 = new SqlCommand(strsql2, conn);
                    SqlDataReader dr = cmd1.ExecuteReader();
                    if (dr.Read())
                    {
                        strUser = dr["AdminName"].ToString().Trim();//如果用户名存在
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('您添加的管理员已存在！');", true);

                    }
                    else //如果不存在就执行添加
                    {
                        dr.Close();
                        if (Session["AdminType"].ToString() == "0")
                        {
                            string strsql = "insert into SyAdmin(AdminName,AdminPwd,AdminType,AdminLoginTime)values('" + strUser + "','" + Pwd + "','" + AdminType + "','" + strTime + "')";
                            SqlCommand addnews = new SqlCommand(strsql, conn);
                            addnews.ExecuteNonQuery();//执行添加
                            conn.Close();
                            conn.Dispose();
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('添加成功！');", true);

                            TextBox3.Text = "";
                            TextBox2.Text = "";
                            TextBox1.Text = "";
                        }
                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('添加失败，您不是超级管理员！');", true);
                        }
                    }
                }
            }
            else　//两次密码如果不一样
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "TestAlert", "alert('两次输入的密码不一致！');", true);
            }
        }
    }
}
